home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / a86a.arc / ZLINES.8 < prev   
Encoding:
Text File  |  1986-06-06  |  3.9 KB  |  97 lines

  1. ;---------------
  2. ;  ZLINES
  3. ;---------------
  4.  
  5. ; This module handles line-oriented standard input.  The main module must leave
  6. ; us with a DATA segment having enough room for 04080 hex bytes; it must call
  7. ; PROCESS_INPUT when it is ready for standard input; it must supply a routine
  8. ; PROCESS_LINE that we call; and it must jump to GOOD_EXIT when it is done.
  9.  
  10. ; We call PROCESS_LINE with SI pointing to CX bytes, constituting the line.
  11. ; The terminating linefeed is counted in the CX bytes.  In addition, DI is
  12. ; left pointing beyond the linefeed.  The main program may clobber any
  13. ; registers.  During the execution of PROCESS_INPUT, we preserve all registers
  14. ; except AL,CX,SI,DI, and the arithmetic flags.
  15.  
  16. ; This module assumes DS=ES=SS at all times.
  17.  
  18. ; We also provide the main entry point MSDOS for operating-system calls; and
  19. ; the entry point USAGE_EXIT for error exits.  The main module must supply
  20. ; a USAGE_MSG of USAGE_MSG_SIZE bytes.
  21.  
  22. DATA SEGMENT
  23.  
  24. IBUFF:
  25.         DB 04000 DUP (?)
  26. THISLINE:
  27.         DB 128 DUP (?)
  28. DATA ENDS
  29.  
  30. GOOD_EXIT:
  31.   MOV AL,0           ; zero indicates program success
  32. EXIT:
  33.   MOV AH,04C         ; MSDOS function number for "EXIT"
  34. MSDOS:
  35.   INT 021            ; this interrupt effects all MSDOS calls
  36.   JNC RET            ; return if there was no error
  37. USAGE_EXIT:
  38.   MOV DX,USAGE_MSG       ; pointer to error message
  39.   MOV CX,USAGE_MSG_SIZE  ; size of error message
  40. ERROR_EXIT:
  41.   MOV BX,2          ; error-output file descriptor
  42.   CALL MWRITE       ; write the error message
  43.   MOV AL,1          ; non-zero status number
  44.   JMP EXIT          ; exit the program
  45.  
  46.  
  47. ; MWRITE writes CX bytes from DS:DX to the open file whose handle is BX.
  48.  
  49. MWRITE:
  50.   MOV AH,040        ; MSDOS code for "WRITE"
  51.   JMP MSDOS         ; get the operating system to do the write
  52.  
  53.  
  54. ; FETCH_INPUT gets more bytes from the physical standard input, and resets
  55. ;   SI and CX to the pointer and count of those bytes.  Return Z if there were
  56. ;   no more bytes.
  57.  
  58. FETCH_INPUT:
  59.   PUSH AX,BX,DX  ; preserve registers across call
  60.   MOV BX,0       ; zero is the open-file number of standard input
  61.   MOV DX,IBUFF   ; point CX to our input buffer
  62.   MOV CX,04000   ; input limit is the size of our buffer
  63.   MOV AH,03F     ; MSDOS function number for READ
  64.   CALL MSDOS     ; read bytes from standard input
  65.   XCHG CX,AX     ; swap actual count into CX
  66.   MOV SI,DX      ; point SI to the buffer pointer
  67.   POP DX,BX,AX   ; restore clobbered registers
  68.   TEST CX,CX     ; set Z flag if there were no bytes read
  69.   RET
  70.  
  71.  
  72. ; PROCESS_INPUT buffers up standard input, one line at a time, into our buffer
  73. ;   THISLINE, then processes the line.  We return when standard input is
  74. ;   exhausted.
  75.  
  76. PROCESS_INPUT:
  77.   CALL FETCH_INPUT    ; get our first buffer-full of input
  78.   JCXZ RET            ; return if there is no more input
  79.   MOV DI,THISLINE     ; we output a line at a time to our buffer
  80. L1:                   ; loop here for each byte copied to our line-buffer
  81.   LODSB               ; fetch the byte
  82.   STOSB               ; store the byte in our buffer
  83.   CMP AL,0A           ; is the byte the line-terminating linefeed?
  84.   JNE >L4             ; jump if not to gather another byte
  85.   PUSH CX,SI          ; preserve input buffer count and pointer
  86.   MOV SI,THISLINE     ; point to the start of this word
  87.   MOV CX,DI           ; point CX beyond the word
  88.   SUB CX,SI           ; calculate the length of the word
  89.   CALL PROCESS_LINE   ; process the input line
  90.   MOV DI,THISLINE     ; restore the gathering-output to the start of the line
  91.   POP SI,CX           ; restore the input buffer's pointer and remaining count
  92. L4:                   ; jump here to process the next line
  93.   LOOP L1             ; count down the input buffer bytes
  94.   CALL FETCH_INPUT    ; if buffer was empty then refill it
  95.   JNZ L1              ; loop if there were more bytes
  96.   RET                 ; no more bytes-- time to return
  97.